package com.example.casper.Experiment2024;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;

import com.example.casper.Experiment2024.view.GameView;

public class GameViewFragment extends Fragment {
    private GameView gameView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_game_view, container, false);

        // 获取 GameView 实例
        gameView = rootView.findViewById(R.id.game_view);

        // 确保 GameView 正常加载
        if (gameView != null) {
            // 可以在这里进行初始化
            gameView.requestFocus();  // 让它获取焦点
        } else {
            Log.e("GameViewFragment", "GameView is null!");
        }

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        // 确保 GameView 获取焦点
        if (gameView != null) {
            gameView.startGame();
            gameView.requestFocus();
        } else {
            Log.e("GameViewFragment", "GameView is still null onResume!");
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        // 暂停游戏
        if (gameView != null) {
            gameView.stopGame();
        }
    }
}
